package com.arice.activity;

import java.util.List;

import com.arice.beans.IncomeInfo;
import com.arice.beans.IncomecodeInfo;
import com.arice.dao.IncomeCodeDao;
import com.arice.dao.IncomeDao;
import com.arice.util.DateHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class IncomeMainActivity extends Activity {

		// 需要适配的数据集合
		private List<IncomeInfo> incomeList;
		// 数据库增删改查操作类
		private IncomeDao dao;
		private IncomeCodeDao codeDao;
		private IncomeInfo income;
		private MyAdapter adapter;
		// ListView
		private ListView ltIncome;
		private TextView tvMonthIncome;
		private Intent intent;
		private int userId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_incomemain);
		
		intent = getIntent();
		userId = intent.getIntExtra("userId", 0);
		dao = new IncomeDao(this);
		incomeList = dao.getAllIncomeByuserId(userId);
		codeDao = new IncomeCodeDao(this);
		
		adapter = new MyAdapter();
		//初始化控件
		ltIncome = (ListView) findViewById(R.id.lvincome);
		tvMonthIncome = (TextView) findViewById(R.id.tvmonthincome);
		// 添加监听器，监听条目点击事件
		ltIncome.setOnItemClickListener(new MyOnItemClickListener());
		ltIncome.setAdapter(adapter);// 给ListView添加适配器(自动把数据生成条目)
		String date = "";
		double incomeMoney = 0.00,allIncome = 0.00;
		for(int i = 0;i <incomeList.size();i++){
			income = incomeList.get(i);
			date = income.getIncomeTime();
			allIncome += income.getIncomeMoney();
			String[] temp =date.split("-");
			if(temp[1].equals(DateHelper.getMonthByNow())){
				incomeMoney += income.getIncomeMoney(); 
			}
		}
		if(incomeMoney==0.0){
			tvMonthIncome.setText("您"+DateHelper.getMonthByNow()+"月还没有记录哦，快来记一笔账吧.");
		}else{
			tvMonthIncome.setText("您当前总收入为"+allIncome+"元\r\n"+DateHelper.getMonthByNow()+"月的收入为"+incomeMoney+"元");
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.incomemainmenu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==R.id.action_addincome){
			intent = new Intent(this, IncomeAddActivity.class);
			intent.putExtra("userId", userId);
			startActivity(intent);
			this.finish();
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	
	// 自定义一个适配器(把数据装到ListView的工具)
		private class MyAdapter extends BaseAdapter {
			
			public int getCount() { // 获取条目总数
				return incomeList.size();
			}

			public Object getItem(int position) { // 根据位置获取对象
				return incomeList.get(position);
			}

			public long getItemId(int position) { // 根据位置获取id
				return position;
			}

			// 获取一个条目视图
			public View getView(int position, View convertView, ViewGroup parent) {
				// 重用convertView
				View item = convertView != null ? convertView : View.inflate(IncomeMainActivity.this, R.layout.incomemainitem, null);

				// 获取该视图中的TextView
				TextView tvIncomeAddtime = (TextView) item.findViewById(R.id.tvincomeaddtime);
				TextView tvIncomeTypeandMoney = (TextView) item.findViewById(R.id.tvincometypeandmoney);
				// 根据当前位置获取Account对象
				income = incomeList.get(position);
				String codeName = codeDao.getCodeNameByCodeId(income.getIncomeCode());
				// 把IncomeInfo对象中的数据放到TextView中
				tvIncomeAddtime.setText(income.getIncomeTime());
				tvIncomeTypeandMoney.setText(codeName+":"+income.getIncomeMoney()+"¥");
			
				return item;
			}
		}

		private class MyOnItemClickListener implements OnItemClickListener {
			private IncomeInfo income;
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// 获取点击位置上的数据
				income = (IncomeInfo) parent.getItemAtPosition(position);
				int codeid = income.getIncomeCode();
				String codeName = codeDao.getCodeNameByCodeId(codeid);
				//点击listView每一项的条目点击事件
				AlertDialog.Builder builder = new Builder(IncomeMainActivity.this);
				builder.setTitle("您想对此记录做什么操作呢？");
				builder.setItems(new String[]{"时间："+income.getIncomeTime(),codeName+":"+income.getIncomeMoney()+"","备注："+income.getRemark()}, null);
				builder.setNeutralButton("删除", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						incomeList.remove(income); // 从集合中删除
						dao.deleteByIncomeId(income.getIncomeId()); // 从数据库中删除
						IncomeMainActivity.this.adapter.notifyDataSetChanged();
					}
				});
				builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						intent = new Intent(IncomeMainActivity.this, IncomeAlterActivity.class);
						intent.putExtra("userId", userId);
						intent.putExtra("incomeId", income.getIncomeId());
						intent.putExtra("incomeMoney", income.getIncomeMoney());
						intent.putExtra("incomeTime", income.getIncomeTime());
						intent.putExtra("remark", income.getRemark());
						startActivity(intent);
						IncomeMainActivity.this.finish();
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						dialog.dismiss();
					}
				});
				builder.show();
			}
		}
}
